package com.yuke.cloud.service.smc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.wmc.dto.StorageSpecInfoDto;
import com.yuke.cloud.service.smc.entity.ShopRecommend;
import com.yuke.cloud.service.smc.service.ShopRecommendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by zn on 2021/03/22.
*/
@Api(value = "WEB - ShopRecommendController", tags = "处理ShopRecommend相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/shop/recommend")
public class ShopRecommendController extends BaseController {
    @Resource
    private ShopRecommendService shopRecommendService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "shopRecommend", value = "该数据记录的POJO") @RequestBody ShopRecommend shopRecommend) {
        if (shopRecommend !=null &&shopRecommend.getSpecId() !=null && shopRecommend.getShopId() != null){
            Integer num = shopRecommendService.selectCount(shopRecommend);
            if (num > 0){
                return WrapMapper.error("你已经收藏该商品");
            }
        }
        Example example = new Example(ShopRecommend.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",shopRecommend.getShopId());
        List<ShopRecommend> shopRecommendList = shopRecommendService.selectByExample(example);
        if (shopRecommendList.size()>30){
            return WrapMapper.error("推荐已达上限");
        }

        return WrapMapper.ok(shopRecommendService.save(shopRecommend));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        shopRecommendService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "shopRecommend", value = "需要修改的内容") @RequestBody ShopRecommend shopRecommend) {
        shopRecommendService.update(shopRecommend);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<ShopRecommend> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        ShopRecommend shopRecommend = shopRecommendService.findById(id);
        return WrapMapper.ok(shopRecommend);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<ShopRecommend>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ShopRecommend> list = shopRecommendService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/condition")
    @ApiOperation(httpMethod = "POST", value = "店铺商品审核,根据店铺状态为1：已申请贩卖，正在审核中，店铺名称或商品名称获取店铺商品")
    public Wrapper<PageInfo<StorageSpecInfoDto>> getListByCondition(@ApiParam(name = "id", value = "记录id") @RequestBody(required = false) ShopRecommend shopRecommend,
                                                                  @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                  @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<StorageSpecInfoDto> list = shopRecommendService.getListByCondition(shopRecommend);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }
}
